package cn.kioye.system;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.kioye.system.ResultAvailableTimeDTO.*;
import cn.kioye.system.ResultAvailablePointDTO.*;

/**
 * @author xx
 * @date 2021-06-02 下午 12:53
 */
public class AppointVaccineService {
    public AppointVaccineService(){
        restTemplate = new RestTemplate();
        objectMapper = new ObjectMapper();
    }

    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;


    /**
     * 获取可接种点
     * @date  2021-06-02 下午 01:30 @author xx
     */
    public List<ResultAvailablePoint> getAvailablePoint(HttpHeaders requestHeaders,String visitcode, String token,Long pid,String vaccCode,String corpCode,String areaId) {
        // 请求参数
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("vaccCrowd", "3");
        requestBody.put("pid", pid);
        requestBody.put("areaId", areaId);
        requestBody.put("numPerPage", 100);
        requestBody.put("pageNum", 1);
        requestBody.put("vaccCode", vaccCode);
        requestBody.put("corpCode", corpCode);
        requestBody.put("outpName", "");
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, requestHeaders);
        ResponseEntity<ResultAvailablePointResponse> response = null;
        for (int i = 0; i < 2; i++) {
            try {
                response = restTemplate.postForEntity("https://szhc.newhealth.com.cn/vacc/service?function=vaccineorder&method=vaccorglist&visitcode=" + visitcode + "&token=" + token, request, ResultAvailablePointResponse.class);
                if (response.getStatusCode() == HttpStatus.OK) {
                    ResultAvailablePointResponse body = response.getBody();
                    if (body != null && body.getRet() == 1) {
                        return body.getData().getList();
                    }
                }
                throw new IllegalStateException(response != null && response.getBody() != null ? response.getBody().getMessage() : "请求服务异常！");
            } finally {
//                log.debug("第{}次请求体：{}", i + 1, request);
//                log.debug("第{}次响应体：{}", i + 1, response);
            }
        }
        return null;
    }

    /**
     * 获取该可接种时间
     * @date  2021-06-02 下午 01:30 @author xx
     */
    public List<ResultAvailableTimePage> getAvailableTime(HttpHeaders requestHeaders,String visitcode, String token, Long pid,String depaId) {
        // 请求参数
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("depaId", depaId);
        requestBody.put("pid", pid);
        requestBody.put("vaccCrowd", "3");
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, requestHeaders);
        ResponseEntity<ResultAvailableTimeResponse> response = null;
        for (int i = 0; i < 2; i++) {
            try {
                response = restTemplate.postForEntity("https://szhc.newhealth.com.cn/vacc/service?function=vaccineorder&method=reservationstockDate&visitcode=" + visitcode + "&token=" + token, request, ResultAvailableTimeResponse.class);
                if (response.getStatusCode() == HttpStatus.OK) {
                    ResultAvailableTimeResponse body =response.getBody();// objectMapper.readValue(stringResponseEntity.getBody(),ResultAvailableTimeResponse.class);
                    if (body != null && body.getRet() == 1) {
                        return body.getData();
                    }
                }
                throw new IllegalStateException(response != null && response.getBody() != null ? response.getBody().getMessage() : "请求服务异常！");
            } finally {
//                log.debug("第{}次请求体：{}", i + 1, request);
//                log.debug("第{}次响应体：{}", i + 1, response);
            }
        }
        return null;
    }

    /**
     * 尝试预约
     * @date  2021-06-02 下午 01:30 @author xx
     */
    public String tryOrder(HttpHeaders requestHeaders,String visitcode, String token,Long pid, String depaId, String depaName, String areaName,
                           String corpCode, String reseDate, String ouatBeginTime, String ouatEndTime, String ouatId,
                           String vaccCode,String reusId,String reusTrueName,String reusIdCard,String reusBirthday,String reusMobile) {
        // 请求参数
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("pid", pid);
        requestBody.put("reusId", reusId);
        requestBody.put("reusTrueName",reusTrueName);
        requestBody.put("reusIdCard",reusIdCard);
        requestBody.put("reusBirthday", reusBirthday);
        requestBody.put("reusMobile", reusMobile);
        requestBody.put("reusSex", 1);
        requestBody.put("depaId", depaId);
        requestBody.put("depaName", depaName);
        requestBody.put("areaName", areaName);
        requestBody.put("vaccCode", vaccCode);
        requestBody.put("vaccName", "新冠疫苗（Vero细胞）");
        requestBody.put("ouatId", ouatId);
        requestBody.put("reseDate", reseDate);
        requestBody.put("ouatBeginTime", ouatBeginTime);
        requestBody.put("ouatEndTime", ouatEndTime);
        requestBody.put("corpCode", corpCode);
        requestBody.put("vaccEffect", "接种本品可刺激机体产生抗新型冠状病毒的免疫力，用于预防新型冠状病毒引rn起的疾病。");
        requestBody.put("vaccCrowd", "3");
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, requestHeaders);
        ResponseEntity<String> response = null;
        for (int i = 0; i < 2; i++) {
            try {
                response = restTemplate.postForEntity("https://szhc.newhealth.com.cn/vacc/service?function=vaccineorder&method=vaccapply&visitcode=" + visitcode + "&token=" + token, request, String.class);
                if (response.getStatusCode() == HttpStatus.OK) {
                    return response.getBody();
                }
                throw new IllegalStateException(response != null && response.getBody() != null ? response.getBody() : "请求服务异常！");
            } finally {
//                log.debug("第{}次请求体：{}", i + 1, request);
//                log.debug("第{}次响应体：{}", i + 1, response);
            }
        }
        return null;
    }

}
